home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / comm / msged400.zip / src / help.c < prev    next >
C/C++ Source or Header  |  1996-06-20  |  3KB  |  177 lines

  1. /*
  2.  *  HELP.C
  3.  *
  4.  *  Written by John Dennis and released to the public domain.
  5.  *
  6.  *  Help subsystem code.
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include "help.h"
  12. #include "msged.h"
  13. #include "main.h"
  14. #include "keys.h"
  15. #include "memextra.h"
  16.  
  17. FILE *help;
  18. HFileHdr Fheader;
  19. HTopicHdr *topics;
  20. char line[255];
  21. int setup;
  22. int CurrTopic;
  23. static int numTopics;
  24.  
  25. void HelpInit(char *fileName)
  26. {
  27.     int i;
  28.  
  29.     setup = 0;
  30.     if ((help = fopen(fileName, "rb")) == NULL)
  31.     {
  32.         return;
  33.     }
  34.     fread(&Fheader, sizeof(HFileHdr), 1, help);
  35.     numTopics = (Fheader.topics[1] << 8) | Fheader.topics[0];
  36.     topics = xcalloc(numTopics, sizeof *topics);
  37.     for (i = 0; i < numTopics; i++)
  38.     {
  39.         fread(&topics[i], sizeof(HTopicHdr), 1, help);
  40.     }
  41.     setup = 1;
  42.     CurrTopic = 0;
  43. }
  44.  
  45. void DisplayPage(long offset, int max)
  46. {
  47.     char *s;
  48.     int done;
  49.     int line_num;
  50.  
  51.     done = 0;
  52.     line_num = 0;
  53.  
  54.     fseek(help, offset, SEEK_SET);
  55.  
  56.     WClear(0, 0, 54, 14, cm[HP_NTXT]);
  57.  
  58.     while (!done)
  59.     {
  60.         if (line_num == max)
  61.             break;
  62.  
  63.         if (fgets(line, 254, help) == NULL)
  64.             break;
  65.  
  66.         if (!strncmp(line, "*Page", 5) || !strncmp(line, "*End", 4))
  67.             break;
  68.  
  69.         if (*line != '\n')
  70.         {
  71.             if ((s = strchr(line, '\n')) != NULL)
  72.                 *s = '\0';
  73.  
  74.             if (!strncmp(line, "*High", 5))
  75.             {
  76.                 s = line + 5;
  77.                 WWriteStr(0, line_num, cm[HP_TTXT], s);
  78.             }
  79.             else
  80.                 WWriteStr(0, line_num, cm[HP_NTXT], line);
  81.         }
  82.         line_num++;
  83.     }
  84. }
  85.  
  86. void DoHelp(int topic)
  87. {
  88.     WND *hWnd, *hCurr;
  89.     long offset[20];
  90.     int depth;
  91.     int page;
  92.     int pages;
  93.     int ch;
  94.     int done;
  95.  
  96.     if (help == NULL)
  97.     {
  98.         return;
  99.     }
  100.     if (topic < 0 || topic > numTopics)
  101.     {
  102.         return;
  103.     }
  104.     fseek(help, topics[topic].offset, SEEK_SET);
  105.  
  106.     if (fgets(line, 254, help) == NULL)
  107.         return;
  108.  
  109.     if (strncmp(line, "*Begin", 6))
  110.         return;
  111.  
  112.     done = 0;
  113.     pages = 1;
  114.     offset[pages - 1] = ftell(help);
  115.  
  116.     while (!done)
  117.     {
  118.         if (fgets(line, 254, help) == NULL)
  119.         {
  120.             return;
  121.         }
  122.         if (!strncmp(line, "*End", 4))
  123.             break;
  124.  
  125.         if (!strncmp(line, "*Page", 5))
  126.         {
  127.             pages++;
  128.             offset[pages - 1] = ftell(help);
  129.         }
  130.     }
  131.  
  132.     fseek(help, offset[0], SEEK_SET);
  133.  
  134.     hCurr = Wtop();
  135.     hWnd = WPopUp(60, 18, INSBDR | SHADOW, cm[HP_BTXT], cm[HP_NTXT]);
  136.  
  137.     WTitle(" Help ", cm[HP_TTXT]);
  138.  
  139.     done = 0;
  140.     page = 0;
  141.     depth = 14;
  142.  
  143.     DisplayPage(offset[page], depth);
  144.  
  145.     while (!done)
  146.     {
  147.         ch = TTGetChr();
  148.         switch (ch)
  149.         {
  150.         case Key_PgDn:
  151.             if (page + 1 < pages)
  152.             {
  153.                 page++;
  154.                 DisplayPage(offset[page], depth);
  155.             }
  156.             break;
  157.  
  158.         case Key_PgUp:
  159.             if (page > 0)
  160.             {
  161.                 page--;
  162.                 DisplayPage(offset[page], depth);
  163.             }
  164.             break;
  165.  
  166.         case Key_Esc:
  167.             done = TRUE;
  168.             break;
  169.  
  170.         default:
  171.             break;
  172.         }
  173.     }
  174.     WClose(hWnd);
  175.     WCurr(hCurr);
  176. }
  177.